package com.accesshollywood.clipboard;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

import javax.swing.text.JTextComponent;

public class ClipboardUtil {
	public static Clipboard	clipboard	= Toolkit.getDefaultToolkit().getSystemClipboard();

	public static void copyText(String str) {
		StringSelection data = new StringSelection(str);

		clipboard.setContents(data, data);
	}

	public static void pasteText(JTextComponent c) {
		Transferable clipData = clipboard.getContents(clipboard);
		if (clipData != null) {
			try {
				if (clipData.isDataFlavorSupported(DataFlavor.stringFlavor)) {
					String s = (String) (clipData.getTransferData(DataFlavor.stringFlavor));
					c.replaceSelection(s);
				}
			} catch (UnsupportedFlavorException ufe) {
				System.err.println("Flavor unsupported: " + ufe);
			} catch (IOException ioe) {
				System.err.println("Data not available: " + ioe);
			}
		}
	}
}
